(function(exports){
    //popup
    exports.bg=chrome.extension.getBackgroundPage();
    //background
    function Background(){
        //监听消息
        this.on=function(callback){
            let callbackBase=callback;
            chrome.runtime.onMessage.addListener(function (request,sender,callback) {
                if(callbackBase(request,sender,callback)===undefined){
                    return true;
                }
            });
        };
        //发送消息
        this.sendTabInfo=function (tabId,sendInfo,response) {
            if(tabId){
                chrome.tabs.sendMessage(tabId, sendInfo, response);
            }else{
                //给当前打开窗口发送信息
                chrome.tabs.query({
                    active:true,
                    currentWindow:true
                },function(e){
                    chrome.tabs.sendMessage(e[0].id, sendInfo, response);
                })
            }
        };
        //查询当前地址
        this.sendInfo=function(info,response){
            chrome.runtime.sendMessage(info,response);
        };
        //发送桌面通知
        let win={};
        //直接向桌面发送一个文本消息
        win.sendInfo=function(icon,title,content){
            let notification = webkitNotifications.createNotification(icon,title,content);
            notification.show();
        };
        //直接向桌面发送一个HTML消息
        win.sendHtml=function(htmlUrl){
            let notification = webkitNotifications.createHTMLNotification(htmlUrl);
            notification.show();
        };
        this.win=win;
        //web操作模块
        let web={};
        //监听发送之前request
        web.beforeRequest=function(callback){
            chrome.webRequest.onBeforeRequest.addListener(callback)
        };
        let js=[{
            type:'',
            url:'',
        }];
    }
    //content_script
    function Content_script(){
        //注入方式运行Js
        this.runJs=function(js){
            let id='zy_temp_'+new Date().getTime();
            let html=`<img src='' id='${id}' onclick='${js}'/>`;
            $('body').append(html);
            id=$('#'+id);
            id.click();
            id.remove();
        };
        //Eval方式运行JS
        this.runJsEval=function(js){
            eval(js);
            let id='zy_temp_'+new Date().getTime();
            let html=`<img src='' id='${id}' onclick='eval(\`${js}\`)'/>`;
            $('body').append(html);
            id=$('#'+id);
            id.click();
            id.remove();
        };
        //直接运行
        this.runThisJs=function(js){
            eval(js)
        };
        //监听消息
        this.on=function(callback){
            let callbackBase=callback;
            chrome.runtime.onMessage.addListener(function (request,sender,callback) {
                if(callbackBase(request,sender,callback)===undefined){
                    return true;
                }
            });
        };
        //像背景页发送信息
        this.sendBgInfo=function(info,response){
            chrome.runtime.sendMessage(info,response);
        };
        //截图可视区域
        this.picture=function(windowId,callback,isJpg){
            let type={
                format:"png"
            };
            if(isJpg){
                type.format="jpeg";
            }
            //callback 参数一：图片数据可以使用img标签显示出来
            chrome.tabs.captureVisibleTab(windowId, type ,callback);
        };
    }
    //其他
    exports.loadScript=function(url, callback) {
        let script = document.createElement("script");
        script.type = "text/javascript";
        if (script.readyState) {
            script.onreadystatechange = function () {
                if (script.readyState == "loaded" || script.readyState == "complete") {
                    script.onreadystatechange = null;
                    if(callback)callback();
                }
            };
        } else {
            script.onload = function () {
                if(callback)callback();
            };
        }
        script.src = url;
        document.body.appendChild(script);
    };
    exports.loadScript$=function (url,callback) {
        $.getScript("test.js").done(function() {
            if(callback)callback();
        }).error(function(err){
            if(callback)callback(err);
        });
    };
    //导出
    exports.util_bg=Background;
    exports.util_cs=Content_script;
    exports.ajax=function(info) {
        $.ajax({
            url:  info.url,
            type: info.type,
            async: info.async ? info.async : false,
            data: info.data,
            dataType: info.dataType,
            success:info.success,
            error:info.error
        });
    };
    //创建通知
    exports.win_info={
        create(){
            chrome.notifications.create('zy_windows_info_id', {
                type:'basic',
                iconUrl:'/icon.png',
                title:'测试标题',
                message:'您的小可爱一以上线'
            }, function(notificationId){
                console.log(notificationId);
            })
        }
    };

})(typeof exports === "object" && exports || this);